package com.cn.bulu.utils.alipay.util;

import org.apache.log4j.Logger;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.cn.bulu.service.impl.MerchantsServiceImpl;
import com.cn.bulu.utils.ClothCurtain.Arith;
import com.cn.bulu.utils.alipay.config.AlipayConfig;

/**
 * 支付点击支付支付时参数的构造 
 * 请求支付宝客户端sdk请求前的参数编辑
 * 2017 04 08
 * @author xgh
 *
 */
public class AlipayParams {
	
	
	/**
	 * @param addPrice
	 * @return
	 */
	public static String getAlipayParams(String orderId, Double totalMoney, Double addPrice,String body,String subject,String passbackParams) {
		
		// 实例化客户端,同时会生成签名
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
				AlipayConfig.app_id, AlipayConfig.app_private_key, "json", "utf-8", AlipayConfig.app_public_key, AlipayConfig.sign_type);
		// 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
		AlipayTradeAppPayRequest alipayRequest = new AlipayTradeAppPayRequest();
		// SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		// 调用支付宝客户端sdk接口要求存在biz_content中的参数
		if(addPrice==null){
			addPrice=0.00;
		}
		if(totalMoney==null){                                                  
			totalMoney=0.00;
		}

		model.setSubject(subject);//商品标题
		model.setTotalAmount(String.valueOf(Arith.add(totalMoney, addPrice)));//订单总金额
		model.setOutTradeNo(orderId);//只能是鹿客订单号，例如 M001708170224708300752117934，不要写成indentId等表id
		//model.setTotalAmount("0.01");
		model.setProductCode("QUICK_MSECURITY_PAY");//销售产品码，商家和支付宝签约的产品码，为固定值
		model.setTimeoutExpress("30m");//超时关闭该订单时间
		model.setBody(body);
		//适用支付宝进行那一项操作0测量,1安装,2维修,3加价悬赏,5充值
		model.setPassbackParams(passbackParams);
		// 鹿客自己业务参数也存放在biz_content中

		// 支付宝客户端请求参数
		alipayRequest.setBizModel(model);
		alipayRequest.setNotifyUrl(AlipayConfig.notifyUrl);
		Logger.getLogger(MerchantsServiceImpl.class).error("后台返回的订单号==="+alipayRequest.toString());
		AlipayTradeAppPayResponse response = null;
		try {
			response = alipayClient.sdkExecute(alipayRequest);
			/* orderInfo = response.getBody(); */
			System.err.println("response.getBody()===>"+response.getBody());
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return response.getBody();
	}

}
